package com.ns.scs.rsr.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.sentinel.LimitType;
import com.ns.core.sentinel.RateLimiter;
import com.ns.core.utils.sg.ResultOk;
import com.ns.scs.rsr.bo.RsrEntryBO;
import com.ns.scs.rsr.bo.RsrPageBO;
import com.ns.scs.rsr.dto.RsrPageDto;
import com.ns.scs.rsr.entry.RsrEntry;
import com.ns.scs.rsr.service.IRsrService;
import com.ns.scs.rsr.vo.ResourcesExtraVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 资源相关操作
 *
 * @author lw
 * @since 2024-11-15
 */
@RestController
@RequestMapping("/rsr")
@RequiredArgsConstructor
@CrossOrigin
public class RsrController {

    private final IRsrService service;

    /**
     * 改
     *
     * @param bo 配置
     */
    @PostMapping("add")
    @ReqLog(module = "资源模块", name = "增")
    public ResultOk add(@RequestBody RsrEntryBO bo) {
        RsrEntry entry = new RsrEntry();
        BeanUtils.copyProperties(bo, entry);
        entry.setSort(service.getMaxSort() + 1);
        return ResultOk.getByBoolean(service.save(entry));
    }

    /**
     * 改
     *
     * @param bo 配置
     */
    @PostMapping("update")
    @ReqLog(module = "资源模块", name = "改")
    public ResultOk update(@RequestBody RsrEntryBO bo) {
        RsrEntry entry = new RsrEntry();
        BeanUtils.copyProperties(bo, entry);
        return ResultOk.getByBoolean(service.updateById(entry));
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "资源模块", name = "逻辑删")
    ResultOk delete(Long id) {
        return ResultOk.getByBoolean(service.removeById(id));
    }

    /**
     * 删 - 根据id列表
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "资源模块", name = "批量删")
    public ResultOk deleteByIds(@RequestBody IdsBO bo) {
        return ResultOk.getByBoolean(service.removeBatchByIds(bo.getIds()));
    }

    /**
     * 查询详情
     *
     * @param id 参数
     */
    @GetMapping("getById")
    @ReqLog(module = "资源模块", name = "详情")
    public ResultOk get(Long id) {
        return ResultOk.getSuccessData(service.getSourceDetailById(id));
    }

    /**
     * 查-集合
     *
     * @param bo 参数
     */
    @PostMapping("getList")
    @ReqLog(module = "资源模块", name = "查-集合")
    @RateLimiter(time = 5, count = 15, limitType = LimitType.IP)
    public ResultOk getList(@RequestBody RsrPageBO bo) {
        RsrPageDto dto = new RsrPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<ResourcesExtraVO> page = service.getList(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }

    /**
     * 查-集合
     *
     * @param bo 参数
     */
    @PostMapping("getSearch")
    @ReqLog(module = "资源模块", name = "分类查询", authFlag = false)
    @RateLimiter(time = 5, count = 50, limitType = LimitType.IP)
    public ResultOk getSearch(@RequestBody RsrPageBO bo) {
        RsrPageDto dto = new RsrPageDto();
        BeanUtils.copyProperties(bo, dto);
        return ResultOk.getSuccessData(service.getSearch(dto));
    }

    /**
     * 点击量
     *
     * @param id 参数
     */
    @GetMapping("click-on")
    @ReqLog(module = "资源模块", name = "点击", authFlag = false)
    public ResultOk clickOn(Long id) {
        RsrEntry entry = service.getById(id);
        entry.setClickNum((Objects.nonNull(entry.getClickNum()) ? entry.getClickNum() : 0) + 1);
        return ResultOk.getByBoolean(service.saveOrUpdate(entry));
    }

    /**
     * 获取详情
     *
     * @param id 参数
     */
    @GetMapping("getResourceById")
    @ReqLog(module = "资源模块", name = "跳转获取详情", authFlag = false)
    public ResultOk getResourceById(Long id) {
        return ResultOk.getSuccessData(service.getResourceById(id));
    }
}
